package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.transfer.transfer
 * Created by wangwenkai on 2017/9/22/16:14
 * <p>
 * <p>
 * 提取创建患者数据(内部使用)
 */
public class ExtractCreationPatientTransfer extends AbstractTransfer {

    public ExtractCreationPatientTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String name = (String) params.get("name");
        String age = (String) params.get("age");
        String dateOfBirth = (String) params.get("dateOfBirth");
        String sex = (String) params.get("sex");
        if ("1".equals(sex)) {
            sex = "男";
        }
        if ("2".equals(sex)) {
            sex = "女";
        }
        String phone = (String) params.get("phone");
        String chargeType = (String) params.get("patientType");

        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Patient").addElement("DataRow");
        dataRowElement.addAttribute("ID_NO", idCard).addAttribute("NAME", name).addAttribute("AGE", age);
        dataRowElement.addAttribute("BIRTHDAY", dateOfBirth).addAttribute("SEX", sex).addAttribute("ADDRESS", "");
        dataRowElement.addAttribute("NATION", "").addAttribute("BIRTH_PLACE", "").addAttribute("ZIP_CODE", "");
        dataRowElement.addAttribute("PHONE_NUMBER_HOME", phone).addAttribute("IDENTITY", "").addAttribute("CHARGE_TYPE", chargeType);
        dataRowElement.addAttribute("MARITAL_STATUS", "");

        Element dataRowElement2 = tplDoc.getRootElement().element("CommitData").addElement("Insurance").addElement("DataRow");
        dataRowElement2.addAttribute("INSUR_NO", "").addAttribute("INSUR_PWD", "");

        Element dataRowElement3 = tplDoc.getRootElement().element("CommitData").addElement("Card").addElement("DataRow");
        dataRowElement3.addAttribute("CARD_NO", idCard).addAttribute("CARD_TYPE", "03").addAttribute("PERSON_NO", idCard);//CARD_TYPE:01是医保卡  03身份证
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            resMap.put("patientId", dataRow.attributeValue("PATIENT_ID"));
            resMap.put("code", executeStatus);
            resMap.put("message", "成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }
}
